import {
  createRouter,
  createWebHistory,
  createWebHashHistory,
} from "vue-router";
import HelloWorld from "../views/main/index.vue";

// 匹配views里面所有的.vue文件,'../views/**/*.vue'作为key,import方法为value
const modules = import.meta.glob("../views/**/*.vue");
// createRouter 创建路由实例
// 配置 history 模式
// 1. history模式：createWebHistory     地址栏不带 #
// 2. hash模式：   createWebHashHistory 地址栏带 #
export const addRoutes = (parentRouteChildren, navItems) => {
  navItems.forEach((item) => {
    // 使用路由最后一个/的名称作为key名
    const route = {
      path: item.path,
      name: item.name,
      component: modules[`../views${item.component}`], // 匹配已加载的组件
      meta: {
        requiresAuth: item.requiresAuth,
        title: item.menuName,
        icon: item.icon,
        affix: item.affix,
      },
    };

    // 如果存在子路由，递归处理
    if (item.children && item.children.length > 0) {
      route.children = [];
      addRoutes(route.children, item.children);
    }

    parentRouteChildren.push(route);
  });
};
const router = createRouter({
  // history: createWebHistory(import.meta.env.BASE_URL),
  history: createWebHashHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: "/",
      name: "home",
      component: HelloWorld,
    },
    {
      path: "/login",
      component: () => import("@/views/login/LoginPage.vue"), // 登录页
    },
    {
      path: "/403",
      component: () => import("@/views/login/403.vue"), //
    },
    {
      path: "/404",
      component: () => import("@/views/login/404.vue"), //
    },
  ],
});
export const DynamicRoute = [
  {
    path: "/",
    name: "Home",
    component: () => import("../views/layout/LayoutContainer.vue"),
    redirect: "/index",
    children: [
      // {
      //   path: '/index',
      //   name: 'Index',
      //   component: () => import('@/views/electric/dashboard.vue'),
      //   meta: {
      //     requiresAuth: false,
      //     title: '首页',
      //     icon: 'Management'
      //   }
      // },
      
    ],
  },
];
export default router
